package com.spark.lola.entity.pur;

import java.math.BigDecimal;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableName;
import com.spark.lola.constant.SupOrderFlag;
import com.spark.lola.constant.SupOrderStatus;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.NumberUtil;
import com.spark.lola.support.mybatis.entity.BaseModel;

import java.util.ArrayList;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 采购单
 * </p>
 * @author dengyejun
 * @since 2020-04-24
 */
@TableName("pur_order_info")
@ApiModel(value="采购单", description="")
public class OrderInfo extends BaseModel implements Serializable {
    private static final long serialVersionUID=1L;

    public OrderInfo(){
        this.no = NumberUtil.getOrderIdByTime("P");
    }

    @ApiModelProperty(value = "标记")
    private int flag = SupOrderFlag.NORMAL;

    @ApiModelProperty(value = "员工")
    @TableField(fill = FieldFill.INSERT)
    private Long creatorId;
    @TableField(fill = FieldFill.INSERT)
    @ExcelProperty("创建时间")
    private Date createTime;

    @ExcelProperty("编码")
    @ApiModelProperty(value = "编码")
    private String no;
    @ApiModelProperty(value = "业务员")
    private Long salesmanId;
    @TableField(exist = false)
    private ShiroUser salesman;



    @ApiModelProperty(value = "收货人")
    private Long consigneeId;
    @TableField(exist = false)
    private ShiroUser consignee;

    @ApiModelProperty(value = "出纳")
    private Long cashierId;

    @TableField(exist = false)
    private ShiroUser cashier;

    @ApiModelProperty(value = "供应商Id")
    private Long supplierId;

    @ApiModelProperty(value = "收款信息")
    private Long bankId;



    @ApiModelProperty(value = "纸厂Id")
    private Long destinationId;

    @ExcelProperty("纸厂")
    @ApiModelProperty(value = "纸厂")
    @TableField(exist = false)
    private String destination;


    @ExcelProperty("车牌号")
    @ApiModelProperty(value = "车牌号")
    private String license;

    @ExcelProperty("供应商")
    @ApiModelProperty(value = "供应商")
    @TableField(exist = false)
    private String supplierName;

    @ExcelProperty("业务员")
    @TableField(exist = false)
    @ApiModelProperty(value = "业务员")
    private String salesmanName;


    @ApiModelProperty(value = "货品链")
    private String productIds;

    @ExcelProperty("货品")
    @ApiModelProperty(value = "货品链")
    private String productNames;

    @ExcelProperty("供货量(吨)")
    @ApiModelProperty(value = "供货量(吨)")
    private BigDecimal quantity=BigDecimal.ZERO;

    @ExcelProperty("扣点(吨)")
    @ApiModelProperty(value = "扣点(吨)")
    private BigDecimal deductQuantity = BigDecimal.ZERO;

    @ExcelProperty("扣点金额")
    @ApiModelProperty(value = "扣点金额")
    @TableField(exist = false)
    private BigDecimal deductAmount = BigDecimal.ZERO;

    @ExcelProperty("扣点率(%)")
    @ApiModelProperty(value = "扣点率(%)")
    private BigDecimal deduct = BigDecimal.ZERO;

    @ExcelProperty("结算量(吨)")
    @ApiModelProperty(value = "结算量(吨)")
    private BigDecimal settleQuantity = BigDecimal.ZERO;

    @ExcelProperty("货款总额")
    @ApiModelProperty(value = "货款总额")
    private BigDecimal totalAmount=BigDecimal.ZERO;

    @ExcelProperty("打包数")
    @ApiModelProperty(value = "打包数")
    private Integer packages=0;

    @ApiModelProperty(value = "运费")
    private BigDecimal freight = BigDecimal.ZERO;

    @ExcelProperty("结算单价")
    @ApiModelProperty(value = "结算单价")
    @TableField(exist = false)
    private BigDecimal univalence = BigDecimal.ZERO;

    @ExcelProperty("结算金额")
    @ApiModelProperty(value = "验收金额")
    private BigDecimal actualAmount = BigDecimal.ZERO;

    @ExcelProperty("付款金额")
    @ApiModelProperty(value = "付款金额")
    private BigDecimal paymentAmount = BigDecimal.ZERO;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "状态")
    private Integer status = SupOrderStatus.WAIT;

    private String startArea;

    private String endArea;

    @ApiModelProperty(value = "发货信息")
    @TableField(exist = false)
    private OrderLocation startLocation;

    @ApiModelProperty(value = "收货信息")
    @TableField(exist = false)
    private OrderLocation endLocation;

    @TableField(fill = FieldFill.INSERT)
    private String creator;
    @TableField(fill = FieldFill.UPDATE)
    private String updator;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @ApiModelProperty(value = "定价时间")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Date pricingTime;

    public String getSalesmanName() {
        if(this.salesman!=null){
            this.salesmanName = this.salesman.getRealName();
        }
        return salesmanName;
    }

    public void setSalesmanName(String salesmanName) {
        this.salesmanName = salesmanName;
    }

    /**
     * 货品明细
     */
    @TableField(exist = false)
    private List<OrderProduct> details=new ArrayList<>();

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Long getBankId() {
        return bankId;
    }

    public void setBankId(Long bankId) {
        this.bankId = bankId;
    }

    public Date getPricingTime() {
        return pricingTime;
    }

    public void setPricingTime(Date pricingTime) {
        this.pricingTime = pricingTime;
    }

    public Integer getPackages() {
        return packages;
    }

    public void setPackages(Integer packages) {
        this.packages = packages;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public String getProductNames() {
        return productNames;
    }

    public void setProductNames(String productNames) {
        this.productNames = productNames;
    }

    public ShiroUser getSalesman() {
        return salesman;
    }

    public void setSalesman(ShiroUser salesman) {
        this.salesman = salesman;
    }

    public ShiroUser getConsignee() {
        return consignee;
    }

    public void setConsignee(ShiroUser consignee) {
        this.consignee = consignee;
    }

    public String getProductIds() {
        return productIds;
    }

    public void setProductIds(String productIds) {
        this.productIds = productIds;
    }

    public List<OrderProduct> getDetails() {
        return details;
    }

    public void setDetails(List<OrderProduct> details) {
        this.details = details;
    }

    public OrderLocation getStartLocation() {
        return startLocation;
    }

    public void setStartLocation(OrderLocation startLocation) {
        this.startLocation = startLocation;
    }

    public OrderLocation getEndLocation() {
        return endLocation;
    }

    public Long getCashierId() {
        return cashierId;
    }

    public void setCashierId(Long cashierId) {
        this.cashierId = cashierId;
    }

    public ShiroUser getCashier() {
        return cashier;
    }

    public void setCashier(ShiroUser cashier) {
        this.cashier = cashier;
    }

    public Long getConsigneeId() {
        return consigneeId;
    }

    public void setConsigneeId(Long consigneeId) {
        this.consigneeId = consigneeId;
    }

    public void setEndLocation(OrderLocation endLocation) {
        this.endLocation = endLocation;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public Long getDestinationId() {
        return destinationId;
    }

    public void setDestinationId(Long destinationId) {
        this.destinationId = destinationId;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getUpdator() {
        return updator;
    }

    public void setUpdator(String updator) {
        this.updator = updator;
    }

    public Long getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(Long creatorId) {
        this.creatorId = creatorId;
    }
    
    public Long getSalesmanId() {
        return salesmanId;
    }

    public void setSalesmanId(Long salesmanId) {
        this.salesmanId = salesmanId;
    }
    
    public Long getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }
    
    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }
    
    public BigDecimal getFreight() {
        return freight;
    }

    public void setFreight(BigDecimal freight) {
        this.freight = freight;
    }
    
    public BigDecimal getActualAmount() {
        return actualAmount;
    }

    public void setActualAmount(BigDecimal actualAmount) {
        this.actualAmount = actualAmount;
    }
    
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getStartArea() {
        return startArea;
    }

    public void setStartArea(String startArea) {
        this.startArea = startArea;
    }

    public String getEndArea() {
        return endArea;
    }

    public void setEndArea(String endArea) {
        this.endArea = endArea;
    }

    public BigDecimal getSettleQuantity() {
        return settleQuantity;
    }

    public void setSettleQuantity(BigDecimal settleQuantity) {
        this.settleQuantity = settleQuantity;
    }

    public BigDecimal getDeductQuantity() {
        return deductQuantity;
    }

    public void setDeductQuantity(BigDecimal deductQuantity) {
        this.deductQuantity = deductQuantity;
    }

    public BigDecimal getPaymentAmount() {
        return paymentAmount;
    }


    public void setPaymentAmount(BigDecimal paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public BigDecimal getDeductAmount() {
        if(deductQuantity.compareTo(BigDecimal.ZERO)==1){
            return totalAmount.subtract(actualAmount);
        }
        return deductAmount;
    }

    public BigDecimal getUnivalence() {
        if(actualAmount!=null && actualAmount.compareTo(BigDecimal.ZERO)==1 && settleQuantity.compareTo(BigDecimal.ZERO)==1){
            univalence = actualAmount.divide(settleQuantity,2,BigDecimal.ROUND_HALF_UP);
        }
        return univalence;
    }

    public void setUnivalence(BigDecimal univalence) {
        this.univalence = univalence;
    }

    public void setDeductAmount(BigDecimal deductAmount) {
        this.deductAmount = deductAmount;
    }

    public BigDecimal getDeduct() {
        return deduct;
    }

    public void setDeduct(BigDecimal deduct) {
        this.deduct = deduct;
    }
}
